﻿@page "/JavaCopyCode"
@using JavaDevToys.Rcl.Context
@using JavaDevToys.Rcl.Entities
@using JavaDevToys.Rcl.Models
@using JavaDevToys.Rcl.Service
@using JavaDevToys.Rcl.Utils
@using global::AutoMapper

<MAlert Dismissible
Type="AlertTypes.Error"
@bind-Value="_alert">
    @errMsg
</MAlert>
@* <h3>常用代码</h3>
<MCard Outlined="true"
Rounded="@("lg")">
    <MList>
        <MListItem OnClick="()=>ShowDataSet=!ShowDataSet">
            <MListItemContent>
                查询值集
            </MListItemContent>
            <MListItemAction>
                <MIcon>
                    @if (ShowDataSet)
                    {
                        @icon_down
                    }
                    else
                    {
                        @icon_right
                    }
                </MIcon>
            </MListItemAction>
        </MListItem>

        <MDivider Class="mx-3"></MDivider>
        @if (ShowDataSet)
        {
            <MCard Class="ma-4">
                <PCopyableText>
                    Map&lt;String, Map&lt;String, String&gt;
                    &gt; codeList=codeSystemServiceApiWorkService.queryDataSetNameByCodeSystemCodes(Lists.newArrayList(
                    CodeSystemEnum.PAY_WAY_CODE.getCode()));
                </PCopyableText>
            </MCard>
        }
        <MListItem OnClick="()=>ShowBaseDisplayName=!ShowBaseDisplayName">
            <MListItemContent>
                设置名称
            </MListItemContent>
            <MListItemAction>
                <MIcon>
                    @if (ShowBaseDisplayName)
                    {
                        @icon_down
                    }
                    else
                    {
                        @icon_right
                    }
                </MIcon>
            </MListItemAction>
        </MListItem>

        <MDivider Class="mx-3"></MDivider>
        @if (ShowBaseDisplayName)
        {
            <MCard Class="ma-4">
                <PCopyableText>
                    baseWorkService.setBaseDisplayName(result);
                </PCopyableText>
            </MCard>
        }

        <MListItem OnClick="()=>ShowDisplayData=!ShowDisplayData">
            <MListItemContent>
                根据语言环境设置名称
            </MListItemContent>
            <MListItemAction>
                <MIcon>
                    @if (ShowBaseDisplayName)
                    {
                        @icon_down
                    }
                    else
                    {
                        @icon_right
                    }
                </MIcon>
            </MListItemAction>
        </MListItem>

        <MDivider Class="mx-3"></MDivider>
        @if (ShowDisplayData)
        {
            <MCard Class="ma-4">
                <PCopyableText>
                    getDisplayData(domain.getName(),domain.get2ndName(),domain.getExtName())
                </PCopyableText>
            </MCard>
        }

    </MList>
</MCard> *@

<h3 class="mt-8">常用代码</h3>
<MCard Class="rounded-lg mb-5"
Outlined="true">
    <MList>
        <MVirtualScroll Class="scroll-show"
        Style="max-height: calc(100vh - 244px);"
        Items="customJavaCodeGroups"
        ItemSize="64">
            <ItemContent>
                <MDivider Class="mx-3"></MDivider>
                <MListItem Ripple="false"
                OnClick="()=>ShowCustomJavaCode(context)">
                    <MListItemIcon Class="mr-5">
                        <MIcon>
                            @if (context.ShowCode){
                                @icon_down
                            }else{
                                @icon_right
                            }
                        </MIcon>
                    </MListItemIcon>
                    <MListItemContent>
                        @if (context.isEditGroup)
                        {
                            <div style="display: flex; align-items: center; gap: 10px;">
                                <MTextField Label="名称" @bind-Value=context.GroupName></MTextField>
                                <MButton Color="primary" Class="mt-2  ml-9" OnClick="()=>SaveCustomJavaCodeGroup(context)">保存</MButton>
                                <MButton Color="primary" Class="mt-2  ml-9" OnClick="()=>CancelEditCustomJavaCodeGroup(context)">取消</MButton>
                            </div>
                        }else{
                            @(context.GroupName)
                        }
                    </MListItemContent>
                    <MListItemAction>
                        <MMenu Context="menuContext"
                        Left="true"
                        Bottom="true"
                        ContentClass="elevation-1"
                        Rounded="@("lg")">
                            <ActivatorContent>
                                <MButton Icon="true"
                                StopPropagation="true"
                                @attributes="@menuContext.Attrs">
                                    <MIcon>mdi-dots-vertical</MIcon>
                                </MButton>
                            </ActivatorContent>
                            <ChildContent>
                                <MList>
                                    <MListItem OnClick="()=>EditCustomJavaCodeGroup(context)">
                                        <MListItemContent>
                                            <MListItemTitle>
                                                编辑
                                            </MListItemTitle>
                                        </MListItemContent>
                                    </MListItem>
                                    <MDivider Class="mx-3"></MDivider>
                                    <MListItem OnClick="()=>DeleteCustomJavaCodeGroup(context)">
                                        <MListItemContent>
                                            <MListItemTitle>
                                                删除
                                            </MListItemTitle>
                                        </MListItemContent>
                                    </MListItem>
                                </MList>
                            </ChildContent>
                        </MMenu>
                    </MListItemAction>
                </MListItem>
                <MDivider Class="mx-3"></MDivider>
                @if (context.ShowCode)
                {
                    @foreach (var item in context.CustomJavaCodes)
                    {
                        @* <MCard Class="ma-4">
                            <MLabel>
                                @item.CustomCodeName
                            </MLabel>
                            <MDivider Class="mx-3"></MDivider>
                            <MLabel>
                                @item.CustomCodeDesc
                            </MLabel>
                            <MDivider Class="mx-3"></MDivider>
                            <PCopyableText>
                                @item.CustomCodeValue
                            </PCopyableText>
                        </MCard> *@
                        <MListItem Ripple="false">
                            <MListItemContent>
                                <MCard Class="ma-4">
                                    
                                    @if (@item.isEdit){
                                        <div style="display: flex; flex-direction: column; gap: 10px; padding: 20px">
                                            <div>
                                                <div style="display: flex; gap: 10px;">
                                                    <MTextField Label="名称" @bind-Value=item.CustomCodeName></MTextField>
                                                    <MTextField Label="描述" @bind-Value=item.CustomCodeDesc></MTextField>
                                                </div>
                                                <MTextarea Label="代码" @bind-Value=item.CustomCodeValue></MTextarea>
                                            </div>
                                            
                                            <div style="display: flex; gap: 10px; justify-content: flex-end;">
                                                <MButton Color="primary" Class="mt-2  ml-9" OnClick="()=>SaveCustomJavaCode(item)">保存</MButton>
                                                <MButton Color="primary" Class="mt-2  ml-9" OnClick="()=>CancelEditCustomJavaCode(item)">取消</MButton>
                                            </div>
                                        </div>
                                    }else{
                                        <MLabel>
                                            @item.CustomCodeName
                                        </MLabel>
                                        <MDivider Class="mx-3"></MDivider>
                                        <MLabel>
                                            @item.CustomCodeDesc
                                        </MLabel>
                                        <MDivider Class="mx-3"></MDivider>
                                        <PCopyableText Style="white-space: pre-wrap;">
                                            @item.CustomCodeValue
                                        </PCopyableText>
                                    }
                                </MCard>
                            </MListItemContent>
                            <MListItemAction>
                                <MMenu Context="menuContext"
                                Left="true"
                                Bottom="true"
                                ContentClass="elevation-1"
                                Rounded="@("lg")">
                                    <ActivatorContent>
                                        <MButton Icon="true"
                                        StopPropagation="true"
                                        @attributes="@menuContext.Attrs">
                                            <MIcon>mdi-dots-vertical</MIcon>
                                        </MButton>
                                    </ActivatorContent>
                                    <ChildContent>
                                        <MList>
                                            <MListItem OnClick="()=>EditCustomJavaCode(item)">
                                                <MListItemContent>
                                                    <MListItemTitle>
                                                        编辑
                                                    </MListItemTitle>
                                                </MListItemContent>
                                            </MListItem>
                                            <MDivider Class="mx-3"></MDivider>
                                            <MListItem OnClick="()=>DeleteCustomJavaCode(item)">
                                                <MListItemContent>
                                                    <MListItemTitle>
                                                        删除
                                                    </MListItemTitle>
                                                </MListItemContent>
                                            </MListItem>
                                        </MList>
                                    </ChildContent>
                                </MMenu>
                            </MListItemAction>
                        </MListItem>
                    }
                    @if (!addCodeFlag)
                    {
                        <div style="padding: 20px">
                            <MCard Class="rounded-lg"
                                   Ripple="false"
                                   Outlined="true">
                                <MListItem OnClick="()=>AddCustomJavaCode(context)" Ripple="false">
                                    <MListItemContent>
                                        添加代码
                                    </MListItemContent>
                                    <MListItemAction>
                                        <MButton Icon>
                                            <MIcon>mdi-plus</MIcon>
                                        </MButton>
                                    </MListItemAction>
                                </MListItem>
                            </MCard>
                        </div>
                    }
                }
            </ItemContent>
        </MVirtualScroll>
    </MList>
</MCard>

@if(!addGroupFlag){
    <MCard Class="rounded-lg"
    Ripple="false"
    Outlined="true">
        <MListItem OnClick="AddCustomJavaCodeGroup" Ripple="false">
            <MListItemContent>
                添加组
            </MListItemContent>
            <MListItemAction>
                <MButton Icon>
                    <MIcon>mdi-plus</MIcon>
                </MButton>
            </MListItemAction>
        </MListItem>
    </MCard>
}

@code {
    private bool _alert = false;
    private string errMsg = string.Empty;

    private string icon_down = "mdi-chevron-down";
    private string icon_right = "mdi-chevron-right";

    private bool addGroupFlag = false;
    private bool addCodeFlag = false;

    [Inject]
    public HisDbContext HisDbContext { get; set; }
    [Inject]
    public IMapper Mapper { get; set; }

    private List<CustomJavaCodeGroup> customJavaCodeGroups= new List<CustomJavaCodeGroup>();

    protected override void OnInitialized()
    {
        base.OnInitialized();
        InitCustomCode();
    }

    private void InitCustomCode(){
        var devtoysCustomcodeGroups = HisDbContext.DevtoysCustomcodeGroups.ToList();
        customJavaCodeGroups = Mapper.Map<List<CustomJavaCodeGroup>>(devtoysCustomcodeGroups);

        foreach (var customJavaCodeGroup in customJavaCodeGroups)
        {
            var devtoysCustomcodes = HisDbContext.DevtoysCustomcodes.Where(w => w.DevtoysCustomcodeGroupId == customJavaCodeGroup.DevtoysCustomCodeGroupId).ToList();
            customJavaCodeGroup.CustomJavaCodes = Mapper.Map<List<CustomJavaCode>>(devtoysCustomcodes);
        }
    }

    private void DeleteCustomJavaCodeGroup(CustomJavaCodeGroup customJavaCodeGroup)
    {
        var customcodeGroup = HisDbContext.DevtoysCustomcodeGroups.Find(customJavaCodeGroup.DevtoysCustomCodeGroupId);
        HisDbContext.DevtoysCustomcodeGroups.Remove(customcodeGroup);
        HisDbContext.SaveChanges();
        InitCustomCode();
    }
    private void AddCustomJavaCode(CustomJavaCodeGroup customJavaCodeGroup){
        customJavaCodeGroup.CustomJavaCodes.Add(new CustomJavaCode() { isEdit = true, DevtoysCustomCodeGroupId = customJavaCodeGroup.DevtoysCustomCodeGroupId });
        addCodeFlag = true;
    }
    /// <summary>
    /// 删除代码
    /// </summary>
    /// <param name="customJavaCode"></param>
    private void DeleteCustomJavaCode(CustomJavaCode customJavaCode)
    {
        var devtoysCustomcode=HisDbContext.DevtoysCustomcodes.Find(customJavaCode.DevtoysCustomCodeId);
        HisDbContext.DevtoysCustomcodes.Remove(devtoysCustomcode);
        HisDbContext.SaveChanges();
        InitCustomCode();
    }
    /// <summary>
    /// 编辑代码
    /// </summary>
    /// <param name="customJavaCode"></param>
    private void EditCustomJavaCode(CustomJavaCode customJavaCode)
    {
        customJavaCode.isEdit = true;
    }
    /// <summary>
    /// 取消编辑代码
    /// </summary>
    /// <param name="customJavaCode"></param>
    private void CancelEditCustomJavaCode(CustomJavaCode customJavaCode)
    {
        customJavaCode.isEdit = false;
        addCodeFlag = false;
        InitCustomCode();
    }
    /// <summary>
    /// 保存代码
    /// </summary>
    /// <param name="customJavaCode"></param>
    private void SaveCustomJavaCode(CustomJavaCode customJavaCode)
    {
        if (!customJavaCode.DevtoysCustomCodeId.HasValue){
            DevtoysCustomcode devtoysCustomcode = Mapper.Map<DevtoysCustomcode>(customJavaCode);
            devtoysCustomcode.DevtoysCustomcodeId = GuidHelper.GenerateUnique19DigitId();
            HisDbContext.DevtoysCustomcodes.Add(devtoysCustomcode);
        }else{
            var devtoysCustomcode = HisDbContext.DevtoysCustomcodes.Find(customJavaCode.DevtoysCustomCodeId);
            devtoysCustomcode.CustomcodeCode = customJavaCode.CustomCodeValue;
            devtoysCustomcode.CustomcodeName = customJavaCode.CustomCodeName;
            devtoysCustomcode.CustomcodeDesc = customJavaCode.CustomCodeDesc;
        }
        HisDbContext.SaveChanges();
        InitCustomCode();
        addCodeFlag = false;
    }

    private void AddCustomJavaCodeGroup()
    {
        customJavaCodeGroups.Add(new CustomJavaCodeGroup(){
            isEditGroup=true,
        });
        addGroupFlag = true;
    }
    /// <summary>
    /// 保存组
    /// </summary>
    private void SaveCustomJavaCodeGroup(CustomJavaCodeGroup customJavaCodeGroup)
    {
        if (!customJavaCodeGroup.DevtoysCustomCodeGroupId.HasValue)
        {
            DevtoysCustomcodeGroup devtoysCustomcodeGroup = Mapper.Map<DevtoysCustomcodeGroup>(customJavaCodeGroup);
            devtoysCustomcodeGroup.DevtoysCustomcodeGroupId = GuidHelper.GenerateUnique19DigitId();
            HisDbContext.DevtoysCustomcodeGroups.Add(devtoysCustomcodeGroup);
        }else{
            var customcodeGroup = HisDbContext.DevtoysCustomcodeGroups.Find(customJavaCodeGroup.DevtoysCustomCodeGroupId);
            customcodeGroup.GroupName = customJavaCodeGroup.GroupName;
        }
        HisDbContext.SaveChanges();
        InitCustomCode();
        addGroupFlag = false;
    }
    /// <summary>
    /// 取消编辑组
    /// </summary>
    private void CancelEditCustomJavaCodeGroup(CustomJavaCodeGroup costomJavaCode)
    {
        costomJavaCode.isEditGroup = false;
        addGroupFlag = false;
        InitCustomCode();
    }
    /// <summary>
    /// 编辑组
    /// </summary>
    /// <param name="costomJavaCode"></param>
    private void EditCustomJavaCodeGroup(CustomJavaCodeGroup costomJavaCode)
    {
        costomJavaCode.isEditGroup = true;
    }

    private void ShowCustomJavaCode(CustomJavaCodeGroup customJavaCodeGroup)
    {
        if (customJavaCodeGroup.isEditGroup)
            return;
        customJavaCodeGroup.ShowCode = !customJavaCodeGroup.ShowCode;
    }
}
